#include "promptwidget.h"
#include <QPainter>
#include "global/myresource.h"
#include "mybtn/mybutton.h"
#include "commondef.h"
#include <QLocale>
#include <QDebug>

#define TEXT_WIDTH      300.0f
#define TEXT_HEIGTH     243.0f
#define TEXT_RC         QRectF(30.0f,200.0f,TEXT_WIDTH,TEXT_HEIGTH)
#define PW_BTN_W        80
#define PW_BTN_H        60
#define TEXT_STR_H      40
#define TEXT_CHAR_NUM   22

PromptWidget::PromptWidget(QWidget *parent,QString str,int tp) :
    QWidget             (parent),
    m_textstr           (str),
    m_tp                (tp)
{
    m_yesbtn = new MyButton(this,QSizeF(PW_BTN_W,PW_BTN_H));
    m_yesbtn->setGeometry(45,350,PW_BTN_W,PW_BTN_H);
    m_yesbtn->setText(tr("yes"),8);
    connect(m_yesbtn,SIGNAL(myclick()),this,SLOT(yesclick()));

    m_nobtn = new MyButton(this,QSizeF(PW_BTN_W,PW_BTN_H));
    m_nobtn->setGeometry(235,350,PW_BTN_W,PW_BTN_H);
    m_nobtn->setText(tr("no"),8);
    connect(m_nobtn,SIGNAL(myclick()),this,SLOT(noclick()));
}

PromptWidget::~PromptWidget()
{
}

void PromptWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);

    p.drawImage(TEXT_RC,
                *MyResource::m_promatbgImage,
                QRectF(0.0f,0.0f,TEXT_WIDTH,TEXT_HEIGTH));

    if(!m_textstr.isEmpty())
    {
        QChar* strlen = m_textstr.data();
        int len = m_textstr.length();
        qDebug()<<"prompt length"<<len;
        int startWord  = 0,endWord =0;
        QChar f(' ');
        int y = 220;
        QLocale locale(QLocale::system());
        int inum = TEXT_CHAR_NUM;
        bool bchina = false;
        if(locale.country() == QLocale::China)
        {
            bchina = true;
            inum = 8;
        }
        do{
            if((len-endWord)<inum)
            {
                endWord = len;
            }
            else
            {
                endWord += inum;
                if(!bchina)
                {
                    while(strlen[endWord] != f)
                    {
                        endWord--;
                    }
                }
            }
            QString s(strlen+startWord,endWord-startWord);

            p.setPen(Qt::white);
            QFont f("Times [Cronyx]",10);//"Times [Cronyx]", "Palatino". "Helvetica [Cronyx]".
            p.setFont(f);

            p.drawText(45,y,TEXT_WIDTH - 10,TEXT_STR_H,Qt::AlignLeft,s);
            startWord = endWord;
            y += TEXT_STR_H;
        }while(endWord<len);
    }
}

void PromptWidget::yesclick()
{
    emit click(m_tp);
}

void PromptWidget::noclick()
{
    emit click(NO_PTP);
}
